package com.xmhmyh.generalframework.project.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xmhmyh.generalframework.common.BaseServiceImpl;
import com.xmhmyh.generalframework.project.sys.dto.CascaderDTO;
import com.xmhmyh.generalframework.project.sys.entity.Location;
import com.xmhmyh.generalframework.project.sys.enums.LocationLvl;
import com.xmhmyh.generalframework.project.sys.mapper.LocationMapper;
import com.xmhmyh.generalframework.project.sys.service.ILocationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author linzm
 * @since 2019-05-20
 */
@Service
public class LocationServiceImpl extends BaseServiceImpl<LocationMapper, Location> implements ILocationService {

    @Override
    public Collection<Location> queryByParentCode(String parentCode) {
        LambdaQueryWrapper<Location> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Location::getParentCode, parentCode);
        lambdaQueryWrapper.orderByAsc(Location::getSequence);
        return list(lambdaQueryWrapper);
    }

    @Override
    public Location getByCode(String code) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Location::getCode, code);
        return getOne(queryWrapper);
    }

    @Override
    public Location getByName(String name, LocationLvl locationLvl) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Location::getName, name);
        queryWrapper.eq(Location::getLvl, locationLvl.getValue());
        return getOne(queryWrapper);
    }

    @Override
    public Location getByName(String parentsCode,String name, LocationLvl locationLvl) {
        LambdaQueryWrapper<Location> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Location::getName, name);
        queryWrapper.eq(Location::getParentCode,parentsCode);
        queryWrapper.eq(Location::getLvl, locationLvl.getValue());
        return getOne(queryWrapper);
    }

    @Override
    public List<CascaderDTO> querySelectHandler(List<String> selectCodeList) {
        // 查询出父级同级
        List<CascaderDTO> allList = new ArrayList<>();
        List<CascaderDTO> sonList = null;
        for (int i = 0; i < selectCodeList.size(); i++) {
            String code = selectCodeList.get(i);
            Location location = getByCode(code);
            Collection<Location> locationCollection = queryByParentCode(location.getParentCode());
            Collection<CascaderDTO> cascaderDTOList = locationCollection.stream().map(l -> {
                CascaderDTO cascaderDTO = new CascaderDTO();
                cascaderDTO.setLabel(l.getName());
                cascaderDTO.setValue(l.getCode());
                cascaderDTO.setParentId(l.getParentCode());
                return cascaderDTO;
            }).collect(Collectors.toList());

            if (i == 0) {
                allList.addAll(cascaderDTOList);
            } else {
                sonList.addAll(cascaderDTOList);
            }

            for (CascaderDTO cascaderDTO : cascaderDTOList) {
                if (i == selectCodeList.size() - 1) {
                    cascaderDTO.setLeaf(true);
                } else {
                    if (cascaderDTO.getValue().equals(code)) {
                        cascaderDTO.setChildren(new ArrayList<>());
                        sonList = cascaderDTO.getChildren();
                    }
                }
            }
        }
        return allList;
    }
}
